﻿using ETC.DLEDLightingSystemPlus.Models;
using ETC.DLEDLightingSystemPlus.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ETC.DLEDLightingSystemPlus.UserControls
{
    /// <summary>
    /// UcTestStation.xaml 的交互逻辑
    /// </summary>
    public partial class UcTestStation : UserControl
    {
        public UcTestStation()
        {
            InitializeComponent();


        }
        public TestStation TestStation
        {
            get
            {
                TestStation result = null;

                this.ThreadSafe(delegate
                {
                    result = this.DataContext as TestStation;
                }, false);

                return result;
            }
        }
        private TestInfo TestInfo
        {
            get
            {
                return this.TestStation.TestInfo;
            }
        }

        //private ConfigInfo ConfigInfo
        //{
        //    get
        //    {
        //        return this.TestStation.ConfigInfo;
        //    }
        //}
        private void BanditButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Control ctl && ctl.Tag != null)
            {
                switch (ctl.Tag.ToString())
                {
                    case "StartTest":

                        this.StartTest();
                        
                        break;
                    case "ResetCount":
                        if (this.ShowDialog("是否确认重置测试计数?", "提示", MessageBoxButton.OKCancel, Bandit.UI.DialogIcon.OK) == MessageBoxResult.OK)
                        {
                            TestInfo.Pass = 0;
                            TestInfo.Fail = 0;
                            TestInfo.Total = 0;
                        }
                        break;
                    case "DeleteStation":
                        {
                            if (this.ShowDialog("Are you sure to delete?", App.PromptText, MessageBoxButton.YesNo, Bandit.UI.DialogIcon.QuestionFace) == MessageBoxResult.Yes)
                            {
                                this.Delete(true);
                            }
                        }
                        break;
                    case "SettingStation":
                        {
                            FrmConfig frm = new FrmConfig(this.TestStation, true);
                            if (frm.ShowDialog().Value)
                            {

                                Console.WriteLine("config baocun");
                                //if (this.BambooBoard != null && this.BambooBoard.IsConnect)
                                //{
                                //    this.TestInfo.EnabledStart = true;
                                //    this.TestStation.Status.Update("Initialize Complate!", MyBrushes.Green);
                                //}

                                //this.TestStation.StationInfo.SlotID = this.ConfigInfo.TestConfig.ProductNumber;
                                //this.RefreshTestItems();
                            }
                            else
                            {
                                Console.WriteLine("加载中...");

                                //this.Loading.Show("加载中...");

                                //Task.Factory.StartNew(() =>
                                //{
                                //    this.Dispatcher.Invoke(new Action(() =>
                                //    {
                                        App.MainViewModel.LoadStations();

                                //    }));

                                //    //Thread.Sleep(1000);

                                //}).ContinueWith(t =>
                                //{
                                //    this.Loading.Hide();
                                //});
                                
                            }
                            //else if (App.IsChangedLimits)
                            //{
                            //    this.TestInfo.TestItems.OnlyUpdateTestConfig(ConfigInfo.TestConfig);
                            //}
                        }
                        break;
                    default:
                        break;
                }
            }
        }


        private void InitTestItemResult()
        {
            foreach (TestItemResult testItemResult in TestInfo.TestItemResults)
            {
                testItemResult.ResultValue = "";
                testItemResult.Result = "";
            }
        }

        private void StartTest()
        {

            InitTestItemResult();

            this.Loading.Show("Testing...");

            Task.Factory.StartNew(() =>
            {
                foreach (TestItemResult testItemResult in TestInfo.TestItemResults)
                {
                    Thread.Sleep(100);

                    testItemResult.ResultValue = "100";
                    testItemResult.Result = "Pass";
                }

            }).ContinueWith(t =>
            {
                this.Loading.Hide();
            });

           
        }

        private void Delete(bool isRefresh)
        {


            App.MainViewModel.RemoveStation(TestStation, isRefresh);
            TestStation.Dispose();
        }
    }
}
